<template>
  <div class="wrap clearfix">
    <div class="location">当前位置：首页<span>&gt;</span><b class="red">购物车</b></div>
   
  <GoodsBody 
  :list="list" 
  :isCheckAll = "isCheckAll"
  @changeChecked = "changeChecked"
  @num1 = "num1"
  @num2 = "num2"
  :leftListLength = "leftListLength"
  :total = "total"
  :discount = "discount"
  :fess = "fess"
  @delBth="delBth"
  @delAll="delAll"
  ></GoodsBody>

  </div>
</template>

<script>
import GoodsBody from '@/components/GoodsBody.vue'

export default {
  components:{
   GoodsBody
  },
  data() {
    return {
      list: [
        {
          name: '凌美Lamy Safari钢笔/签字笔T52黑色墨水<',
          id: 0,
          oldPrice: 158.00,
          price: 129.00,
          count: 0,
          img: 'https://img-blog.csdnimg.cn/20200520143727974.jpg',
          isSelected: true
        },
        {
          name: '凌美Lamy Safari钢笔/签字笔T52黑色墨水<',
          id: 1,
          oldPrice: 158.00,
          price: 145.00,
          count: 0,
          img: 'https://img-blog.csdnimg.cn/20200520143752215.jpg',
          isSelected: false
        },
        {
          name: '德国进口 凌美(LAMY)签字笔钢笔墨水水笔专用一次性墨水胆笔芯5支一盒T10德国进口',
          id: 2,
          oldPrice: 158.00,
          price: 130.00,
          count: 0,
          img: 'https://img-blog.csdnimg.cn/20200520143746317.jpg',
          isSelected: true
        }
      ]
    }
  },
  methods:{
    // 删除功能
    delBth(id){
      this.list = this.list.filter(item=>item.id !== id)
    },
  //全选反选功能
    changeChecked(flag){
      this.list.forEach(item => item.isSelected = flag)
    },
    // +号
    num1(id){
      this.list.forEach(item=>{
        if(item.id === id){
          item.count ++
        }
      }) 
    },
    // —号
    num2(id){
      this.list.forEach(item=>{
        if(item.id === id){
          if(item.count > 0){
            item.count --
          }else{
            item.count = 0
          }
          
        }
      }) 
    },
    // 删除所有商品
    delAll(){
      this.list = this.list.filter((item)=>!item.isSelected)
    }
  },
  computed:{
    //全选功能
     isCheckAll(){
      return this.list.every(item => item.isSelected)     
    },
    // 计算总价
    total(){
      return this.list.reduce((prev,item)=>{
        return prev + item.count * item.oldPrice
      },0)
    },
    // 已选商品
    leftListLength(){
      return  this.list.filter(item=>item.isSelected).length
    },
    // 优惠价格
    discount(){
      return  this.list.reduce(()=>{
        return  this.fess - this.total
      },0)
    },
    // 
    fess(){
      return this.list.reduce((prev,item)=>{
        return (prev + item.price * item.count)
        },0)
    }
  }
}
</script>

<style>
/* 公共样式 */
.clearfix {
  content: '';
  display: block;
  clear: both;
}

.red {
  color: #f30213;
}

.fl {
  float: left;
}

.fr {
  float: right;
}

/* 清除默认样式 */
* {
  margin: 0px;
  padding: 0px;
  font-size: 14px;
}

a {
  text-decoration: none;
  color: #333;
}

input {
  outline: none;
}

.wrap {
  width: 1180px;
  margin: 0 auto;
}

.wrap .location {
  padding: 10px 0;
  border-bottom: 1px solid #ccc;
  margin-bottom: 20px;
}

.cartTable {
  border: 1px solid #ccc;

}

.cartTable tr.title {
  background-color: rgb(241, 243, 244);
  font-weight: bold;
}

.cartTable tbody tr td {
  /* border: 1px solid rgb(245, 245, 245); */
  padding: 10px;
}

/* 原价 */
.cartTable tbody tr td .tdl {
  text-decoration: line-through;
  color: #999;
}

/* 商品数量 */
.cartTable tr .amount .unum {
  width: 70px;
  height: 30px;
  box-sizing: border-box;
  text-align: center;
  font-size: 16px;
  float: left;
}

/* 数量加减按钮 */
.cartTable tr .amount a {
  display: block;
  width: 30px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  font-size: 18px;
  border: 1px solid #ccc;
  box-sizing: border-box;
}

.cartTable tr .amount a.Increase {
  border-right: none;
  float: left;
}

.cartTable tr .amount a.Reduce {
  border-left: none;
  float: left;
}

.cartTable tr.count .jiesuan .right {
  text-align: right;

}

.cartTable tr.count .jiesuan .right p {
  margin: 5px 0;
}

.cartTable tr.count .jiesuan .right .pay {
  display: block;
  width: 160px;
  height: 50px;
  line-height: 50px;
  background-color: #f30213;
  color: #fff;
  font-weight: bold;
  text-align: center;
  font-size: 20px;
}
</style>
